package cn.tellfish.studydemo.demo;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierTest {


    public static void main(String[] args) {

        int concurrency = 100;
        CyclicBarrier cb = new CyclicBarrier(concurrency, new Runnable() {
            @Override
            public void run() {
                System.out.println("~~~~~~~~~~~~~准备完成");
            }
        });
        Random rd = new Random();
        for (int i = 0; i < concurrency; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(rd.nextInt(10000));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + " 准备就绪");
                    try {
                        cb.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }

}
